package com.shop.mall.dao.impl;

import com.shop.mall.utils.MysqlDb;
import com.shop.mall.dao.UserDao;
import com.shop.mall.entity.User;
import com.shop.mall.utils.Pages;
import org.apache.commons.lang3.StringUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static com.shop.mall.utils.MysqlDb.getConnection;

/**
 * @author admin
 * @version 1.0
 * @date 2019/4/23
 */
public class UserDaoImpl implements UserDao {
    @Override
    public List<User> select(int id) throws Exception {
        Pages pages = null;
        Connection connection = MysqlDb.getConnection();
        List<User> list = new ArrayList<User>();
        PreparedStatement ps = connection.prepareStatement("select * from user where id = ? and deleteFlag = 0");
        ps.setObject(1, id);
        System.out.println("执行sql:" + ps);
        ResultSet rs = ps.executeQuery();
        while (rs.next()) {
            User user = new User();
            user.setId(rs.getLong("id"));
            user.setName(rs.getString("name"));
            user.setPassword(rs.getString("password"));
            user.setEmail(rs.getString("email"));
            user.setTel(rs.getString("tel"));
            user.setSex(rs.getString("sex"));
            user.setRole(rs.getString("role"));
            user.setStatus(rs.getString("status"));
            list.add(user);
            pages = new Pages();
            pages.setCurrentPage(1);
            pages.setTotalPage(5);
            pages.setTotalRecord(list.size());
            pages.setList(list);
        }
        MysqlDb.closeAll(rs, ps, connection);
        return list;
    }

    @Override
    public Boolean login(String username, String password) throws Exception {
        if (StringUtils.isBlank(username) || StringUtils.isBlank(password)) {
            throw new RuntimeException("请填写用户名和密码");
        }
        Boolean flag = false;
        String sql = "select * from User where name=? and password = ? and deleteFlag = 0";
        PreparedStatement ps = getConnection().prepareStatement(sql);
        ps.setObject(1, username);
        ps.setObject(2, password);
        System.out.println("执行sql:" + ps);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
           /* System.out.println("状态:" + rs.next()
                    + "\n username:" + rs.getString("name")
                    + "\n password:" + rs.getString("password"));*/
            flag = true;
        }
        System.out.println(flag ? "查询到用户:" + rs.getString("name") : "查无此人");
        //MysqlDb.closeAll(rs, ps, MysqlDb.getConnection());
        return flag;
    }

    @Override
    public int regist(User user) throws Exception {
        if (user == null) {
            throw new RuntimeException("请填写用户信息");
        }
        Connection conn = getConnection();
        String sql = "insert into user(id,name,password,tel,email,sex,role,status) values (?,?,?,?,?,?,?,?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setObject(1, user.getId());
        ps.setObject(2, user.getName());
        ps.setObject(3, user.getPassword());
        ps.setObject(4, user.getTel());
        ps.setObject(5, user.getEmail());
        ps.setObject(6, user.getSex());
        ps.setObject(7, user.getRole());
        ps.setObject(8, user.getStatus());
        System.out.println("执行sql:" + ps);
        int line = ps.executeUpdate();
        System.out.println(line > 0 ? "保存成功,更新记录行数:" + line : "保存失败,更新记录行数:" + line);
        return line;
    }

    @Override
    public int update(User user) throws Exception {
        if (user == null) {
            throw new RuntimeException("请填写要修改的用户信息");
        }
        Connection conn = getConnection();
        String sql = "update user set name = ?, password = ?, tel = ?, email = ?, sex = ?, role = ?,status = ? where id = ? and deleteFlag = 0";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setObject(1, user.getName());
        ps.setObject(2, user.getPassword());
        ps.setObject(3, user.getTel());
        ps.setObject(4, user.getEmail());
        ps.setObject(5, user.getSex());
        ps.setObject(6, user.getRole());
        ps.setObject(7, user.getStatus());
        ps.setObject(8, user.getId());
        System.out.println("执行sql:" + ps);
        int line = ps.executeUpdate();
        System.out.println(line > 0 ? "修改成功,更新记录行数:" + line : "修改失败,更新记录行数:" + line);
        return line;
    }

    @Override
    public int delete(int id) throws Exception {
        String sql = "update user set deleteFlag = 1 where id = ?";
        PreparedStatement ps = getConnection().prepareStatement(sql);
        ps.setObject(1, id);
        System.out.println("执行sql:" + ps);
        int line = ps.executeUpdate();
        System.out.println(line > 0 ? "修改成功,更新记录行数:" + line : "修改失败,更新记录行数:" + line);
        MysqlDb.closeAll(null, ps, getConnection());
        return line;
    }

    @Override
    public List<User> getuserList() throws Exception {
        List<User> list = new ArrayList<User>();
        Connection conn = getConnection();
        try {
            PreparedStatement ps = conn.prepareStatement("select * from user where deleteFlag = 0");
            System.out.println("执行sql:" + ps);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setName(rs.getString("name"));
                user.setTel(rs.getString("tel"));
                list.add(user);
            }
            MysqlDb.closeAll(rs, ps, conn);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Boolean webLogin(String email, String password) throws Exception {
        if (StringUtils.isBlank(email) || StringUtils.isBlank(password)) {
            throw new RuntimeException("请填写用户邮箱和密码");
        }
        Boolean flag = false;
        String sql = "select * from User where email=? and password = ? and deleteFlag = 0";
        PreparedStatement ps = getConnection().prepareStatement(sql);
        ps.setObject(1, email);
        ps.setObject(2, password);
        System.out.println("执行sql:" + ps);
        ResultSet rs = ps.executeQuery();
        if (rs.next()) {
           /* System.out.println("状态:" + rs.next()
                    + "\n username:" + rs.getString("name")
                    + "\n password:" + rs.getString("password"));*/
            flag = true;
        }
        System.out.println(flag ? "查询到用户:" + rs.getString("email") : "查无此人");
        //MysqlDb.closeAll(rs, ps, MysqlDb.getConnection());
        return flag;
    }
}
